package com.xunqi.sell.controller;

import com.xunqi.sell.dto.OrderDTO;
import com.xunqi.sell.form.ProductForm;
import com.xunqi.sell.pojo.ProductCategory;
import com.xunqi.sell.pojo.ProductInfo;
import com.xunqi.sell.service.CategoryService;
import com.xunqi.sell.service.ProductService;
import com.xunqi.sell.util.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  卖家端商品
 */

@Controller
@RequestMapping(value = "/seller/product")
public class SellProductController {

    @Resource
    private ProductService productService;

    @Resource
    private CategoryService categoryService;

    /**
     *  商品列表
     * @param page
     * @param size
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

        PageRequest request = new PageRequest(page - 1, size);
        Page<ProductInfo> productInfoPage = productService.findAll(request);
        map.put("productInfoPage", productInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("product/list", map);
    }


    /**
     *  商品上架
     * @param productId
     * @param map
     * @return
     */
    @GetMapping(value = "/on_sale")
    public ModelAndView onSale(@RequestParam("productId") String productId,
                               Map<String,Object> map) {

        try {
            productService.onSale(productId);
        } catch (Exception e) {
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }


    /**
     *  商品下架
     * @param productId
     * @param map
     * @return
     */
    @GetMapping(value = "/off_sale")
    public ModelAndView offSale(@RequestParam("productId") String productId,
                               Map<String,Object> map) {

        try {
            productService.offSale(productId);
        } catch (Exception e) {
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);

    }


    @GetMapping(value = "/index")
    public ModelAndView index(@RequestParam(value = "productId",required = false) String productId,
                      Map<String,Object> map) {

        if (!StringUtils.isEmpty(productId)) {
            ProductInfo productInfo = productService.findOne(productId);
            map.put("productInfo",productInfo);
        }

        //查询所有的类目
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList",categoryList);

        return new ModelAndView("product/index",map);

    }


    /**
     *  保存/更新
     * @param form
     * @param result
     * @param map
     * @return
     */
    @PostMapping(value = "/save")
//    @CachePut(cacheNames = "product",key = "123")
//    @CacheEvict(cacheNames = "product",key = "123")
    public ModelAndView save(@Valid ProductForm form, BindingResult result,Map<String,Object> map) {

        //判断传过来的参数是否有问题进行效验
        if (result.hasErrors()) {
            map.put("msg",result.getFieldError().getDefaultMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }

        ProductInfo productInfo = new ProductInfo();

        try {
            //如果传过来的productId不是空的执行修改
            if (!StringUtils.isEmpty(form.getProductId())) {
                //先查一遍
                productInfo = productService.findOne(form.getProductId());
            } else {
                form.setProductId(KeyUtil.genUniqueKey());
            }
            BeanUtils.copyProperties(form,productInfo);
            productInfo.setId(form.getProductId());
            productService.save(productInfo);
        } catch (Exception e) {
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }

        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }

}
